<?php

class My_Work_Controller extends Zend_Controller_Action {

	public function init() {
		if ('login' != Zend_Controller_Front::getInstance()->getRequest()->getControllerName()) {
			$this->checkLogin();
			$this->initNavigation();
		}

		$this->initDoctype();
		$this->initDefaultCss();
		$this->initAdmin();
	}

	private function checkLogin() {
		$auth = Zend_Auth::getInstance();

		if (!$auth->hasIdentity()) {
			$this->_helper->redirector->gotoSimpleAndExit('index', 'login');
		}

		$this->view->adminUser = Zend_Auth::getInstance()->getIdentity();
	}

	private function initDefaultCss() {
		$this->view->headLink()->appendStylesheet('/Lib/bootstrap-twitter/bootstrap.min.css');
		$this->view->headLink()->appendStylesheet('/Lib/css/a.css');
	}

	private function initDoctype() {
		$doctypeHelper = new Zend_View_Helper_Doctype();
		$doctypeHelper->doctype('HTML5');
	}

	private function initNavigation() {
		$navArray = array(
			array(
				'controller' => 'index',
				'label' => 'Home',
			),
			array(
				'controller' => 'pessoa',
				'label' => 'Pessoa',
			),
			array(
				'controller' => 'material',
				'label' => 'Material',
			),
		);

		foreach ($navArray as &$navArrayItem) {
			$navArrayItem['module'] = 'work';
		}

		$container = new Zend_Navigation($navArray);

		$this->view->navigation($container);
		$this->view->navigation()->menu()->setUlClass('nav');
	}

	public function addJQuery() {
		$this->view->headLink()->appendStylesheet($this->_dirJs . '/jq-ui-lightness/jq-ui-lightness.css', 'screen');

		$this->view->headScript()->prependFile($this->_dirJs . '/jq-ui.js');
		$this->view->headScript()->prependFile($this->_dirJs . '/jq.js');
	}

	/**
	 * Retorna o objeto Grid Table
	 *
	 * @param string $gridName Nome da grid
	 * @param Zend_Config|array $options Opções da grid
	 * @param string $id Id da Grid
	 * @return ZFWork_Grid_Deploy_Table
	 */
	public function getGrid($gridName, $options=array(), $id='') {
		ZFWork_Grid::$_loadDefaultTemplate = false;
		$grid = ZFWork_Grid::myFactory($gridName, $options, $id);
		
		$grid->addTemplateDir(APPLICATION_PATH . '/My/Work/Grid/Template', 'My_Work_Grid_Template', 'table');
		$grid->setTemplate('Table', 'table');

		return $grid;
	}

}